package plgdbv1

import (
	"dgo/MyProj/PrjDBUtils"
	"dgo/MyProj/PrjHttpUtils"
	"dgo/goutils/gohttp"
	"dgo/goutils/utils"
	"gitee.com/ymofen/supervalue"
	"net/http"
)

type PlgDBRequest struct {
	PlgHttpRec
}

type PlgDBBase struct {
	PlgBase

	msglst *utils.SyncCycleList

	// 是否运行跨域
	Http_allow_cross_access bool

	Default_datasourceid string

	// 注册的uri
	api_uri_map                  utils.GSyncMapGroup
	api_prefix                   string
	OnRegisterDBHandlerApiFunc   func(api_prefix string)
	OnUnRegisterDBHandlerApiFunc func(api_prefix string)
}

func (this *PlgDBBase) QueryList(sqlscript string, lst *wrapper.SuperValue) {

}

func (this *PlgDBBase) LogMsglst(s string, args ...interface{}) {
	this.msglst.LogMsg(s, args...)
}

func (this *PlgDBBase) innerRelease() {
	if len(this.api_prefix) > 0 {
		gohttp.UnHandle(this.api_prefix)
		gohttp.UnHandle(this.api_prefix + "sys/res/reload")
		if this.OnUnRegisterDBHandlerApiFunc != nil {
			this.OnUnRegisterDBHandlerApiFunc(this.api_prefix)
		}
	}

	this.api_uri_map.Range(func(key, value interface{}) bool {
		gohttp.UnHandle(value.(string))
		return true
	})
}

func (this *PlgDBBase) Close() error {
	this.innerRelease()
	return nil
}

func (this *PlgDBBase) NewPlgDBRequest(w http.ResponseWriter, r *http.Request) *PlgDBRequest {
	rval := &PlgDBRequest{}
	rval.ReqR = r
	rval.RespW = w
	rval.Resp = wrapper.NewSVObject()
	rval.EncodeJSONEscape = true

	if utils.DEBUG_MODE == 1 {
		rval.EncodeJSONEscape = false
	}

	if this.Http_allow_cross_access {
		PrjHttpUtils.AllowCrossAccessSet(w)
	}

	rval.CheckDecodeBody()

	return rval
}

func (this *PlgDBBase) innerReloadApiPrefix(strv string) {
	if this.api_prefix != strv {
		if len(this.api_prefix) > 0 {
			if this.OnUnRegisterDBHandlerApiFunc != nil {
				this.OnUnRegisterDBHandlerApiFunc(this.api_prefix)
			}
		}
		this.api_prefix = strv
		if len(this.api_prefix) > 0 {
			if this.OnRegisterDBHandlerApiFunc != nil {
				this.OnRegisterDBHandlerApiFunc(this.api_prefix)
			}
		}
	}
}

/*
*

	  {
		  "id": "db-helper",
		  "type": "db.base",
		  "conf": {
			"datasource":[
			  {"id":"main", "drivername":"mysql", "connstring":"admin:WXvrs@admin!@#1@tcp(119.96.188.188:3306)/dcloud?charset=utf8&parseTime=true&loc=Local"}
			],
			"http-allow-cross-access": true,  // 允许跨域访问
	        "api-prefix":"/api/web/",     // db处理api [RES]/list, update, del,
	        "default-datasourceid":"main"
	  }
*/
func (this *PlgDBBase) ReloadConf(conf *wrapper.SuperValue) (err error) {
	if this.msglst == nil {
		this.msglst = utils.NewSyncCycleList()
	}

	PrjDBUtils.LoadDataSourceConfig(conf.ValueByName("datasource"))

	conf.CheckExistsStrFunc("default-datasource", func(strv string) {
		this.Default_datasourceid = strv
	})

	conf.CheckExistsStrFunc("default-datasourceid", func(strv string) {
		this.Default_datasourceid = strv
	})

	conf.CheckExistsStrFunc("api-prefix", func(strv string) {
		this.innerReloadApiPrefix(strv)
	})

	return
}

func (this *PlgDBBase) UnRegisterAPI(key string) {
	uri := this.api_uri_map.Get(key, "").(string)
	if len(uri) > 0 {
		gohttp.UnHandle(uri)
	}
}

func (this *PlgDBBase) CheckRegisterApiURIEx(urisuffix string, fn func(req *PlgDBRequest)) {
	this.CheckRegisterApi(urisuffix, this.api_prefix+urisuffix, func(w http.ResponseWriter, r *http.Request) {
		req := this.NewPlgDBRequest(w, r)
		fn(req)
	})
}

func (this *PlgDBBase) CheckRegisterApiURI(urisuffix string, fn http.HandlerFunc) {
	this.CheckRegisterApi(urisuffix, this.api_prefix+urisuffix, fn)
}

func (this *PlgDBBase) CheckRegisterApiEx(uri string, fn http.HandlerFunc) {
	this.CheckRegisterApi(uri, uri, fn)
}

func (this *PlgDBBase) CheckRegisterApi(key string, uri string, fn http.HandlerFunc) {
	uri0 := this.api_uri_map.Get(key, "").(string)
	if uri0 != uri {
		if len(uri0) > 0 {
			gohttp.UnHandle(uri)
		}
		this.api_uri_map.Set(key, uri)
		if len(uri) > 0 {
			gohttp.HandleFunc(uri, fn)
		}
	}
}
